import { IGlslProgram, glslup450Program } from '@feng3d/glslup';
import { glsl2wgsl } from './glsl2wgsl';

/**
 * 转换glsl着色程序为wgsl着色程序。
 *
 * @param program glsl着色程序。
 * @param handler 可用于特殊手动处理某些 glslup 无法处理的问题。
 * @returns 转换后的wgsl着色程序。
 */
export function glsl2wgslProgram(program: IGlslProgram, handler?: { vertex450Handler: (code: string) => string, fragment450Handler: (code: string) => string })
{
    const glsl450Program = glslup450Program(program);

    const vertex450Handler = handler?.vertex450Handler ?? ((code: string) => code);
    const fragment450Handler = handler?.fragment450Handler ?? ((code: string) => code);

    const vertex = glsl2wgsl(vertex450Handler(glsl450Program.vertex));
    const fragment = glsl2wgsl(fragment450Handler(glsl450Program.fragment));

    return { vertex: vertex.code, fragment: fragment.code, layoutInfo: glsl450Program.layoutInfo };
}
